<template>
    <view 
        id="amap"
        style="width: 100%; height: 100%"
        :points="points"
        :change:points="aMapRenderJs.receivePoints"
    >
    </view>
</template>

<script>
export default {
    props: {
        startPoint: {
            type: Array,
            default: () => [],
        },
        endPoint: {
            type: Array,
            default: () => [],
        },
    },
    computed: {
        points () {
            return { startPoint: this.startPoint, endPoint: this.endPoint };
        }
    }
};
</script>

<script module="aMapRenderJs" lang="renderjs">
const A_MAP_KEY = "9e005a1a3c0301babc2e71f27a686fad";
const A_MAP_SECRET_KEY = "";

export default {
	data(){
		return{
			marker:null,
			data:[]
		}
	},
    mounted () {
        if (typeof window.AMap === 'function') {
            this.initAmap();
        } else {
            window._AMapSecurityConfig = {
                securityJsCode: A_MAP_SECRET_KEY,
            };
            const script = document.createElement('script');
            script.src = `https://webapi.amap.com/maps?v=1.4.15&key=${A_MAP_KEY}&plugin=AMap.Walking`;
            script.onload = this.initAmap.bind(this);
            document.head.appendChild(script);
        }
    },
    methods: {
		WGS84ToGCJ(wgLon,wgLat){
		     var pi = 3.14159265358979324
		     var ee = 0.00669342162296594323
		     var a = 6378245.0
		 
		     var dLat = this.transformLat(wgLon-105.0, wgLat-35.0)
		     var dLon = this.transformLon(wgLon-105.0, wgLat-35.0)
		     let radLat = wgLat / 180.0 * pi;
		     var magic = Math.sin(radLat);
		     magic = 1 - ee * magic * magic;
		     var sqrtMagic = Math.sqrt(magic);
		     dLat = (dLat * 180.0) / ((a * (1-ee)) / (magic * sqrtMagic) * pi);  
		     dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi); 
		     var mgLat = parseFloat(wgLat) + parseFloat(dLat);  
		     var mgLon = parseFloat(wgLon) + parseFloat(dLon); 
		     var d = [mgLon,mgLat]
		     return d
		 },
		 //转为纬度
		 transformLat(x,y){
		     var pi = 3.14159265358979324;
		     var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));  
		     ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;  
		     ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;  
		     ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;  
		     return ret
		 },
		 //转换经度
		 transformLon(x,y){
		     var pi = 3.14159265358979324;
		     var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));  
		     ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;  
		     ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;  
		     ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;  
		     return ret
		 }, 
		getTime(){
			const currentTime = new Date();
			const year = currentTime.getFullYear();
			const month = currentTime.getMonth() + 1; // 月份从0开始，因此需要加1
			const day = currentTime.getDate();
			const hours = currentTime.getHours();
			const minutes = currentTime.getMinutes();
			const seconds = currentTime.getSeconds();
			this.specificDate=`${year}-${month>9?month:'0'+month}-${day>9?day:'0'+day}`
			this.clockTime=`${hours>9?hours:'0'+hours}:${minutes>9?minutes:'0'+minutes}:${seconds>9?seconds:'0'+seconds}`
			
			//this.clockType=hours<9?1:2
		},
        initAmap () {
            const map = new AMap.Map('amap', {
                resizeEnable: true,
            });
			
			
			this.marker = new AMap.Marker({
			    icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
			    position: [116.41024349945722, 39.916405281607325],
			    anchor:'bottom-center'
			});
			
			// //实例化城市查询类
			//         var citysearch = new AMap.CitySearch();
			//         //自动获取用户IP，返回当前城市
			//         citysearch.getLocalCity(function(status, result) {
			//             if (status === 'complete' && result.info === 'OK') {
			//                 if (result && result.city && result.bounds) {
			//                     var cityinfo = result.city;
			//                     var citybounds = result.bounds;
			//                     document.getElementById('info').innerHTML = '您当前所在城市：'+cityinfo;
			//                     //地图显示当前城市
			//                     map.setBounds(citybounds);
			//                 }
			//             } else {
			//                 document.getElementById('info').innerHTML = result.info;
			//             }
			//         });
			
			
			
			
			
			
			
			
			 
            
        },
        makeWalking() {
            if(!this.points.startPoint.length || !this.points.endPoint.length) {
                return;
            };

            this.walking.search(
                this.points.startPoint,
                this.points.endPoint,
                function(status, result) {
                    if (status === 'complete') {
                        console.log('绘制步行路线完成');
                    } else {
                        console.error('步行路线数据查询失败' + result);
                    }
                }
            );
        },
        receivePoints(newVal) {
            console.log(`坐标更新：${JSON.stringify(newVal)}`);
          
        },
    },
};
</script>
